package com.restkeeper.controller.credit;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.restkeeper.exception.BussinessException;
import com.restkeeper.response.page.PageVO;
import com.restkeeper.store.entity.Credit;
import com.restkeeper.store.entity.CreditCompanyUser;
import com.restkeeper.store.entity.CreditLogs;
import com.restkeeper.store.entity.CreditRepayment;
import com.restkeeper.store.service.ICreditLogService;
import com.restkeeper.store.service.ICreditRepaymentService;
import com.restkeeper.store.service.ICreditService;
import com.restkeeper.utils.BeanListUtils;
import com.restkeeper.vo.credit.CreditCompanyUserVO;
import com.restkeeper.vo.credit.CreditRepaymentVO;
import com.restkeeper.vo.credit.CreditVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/credit")
@Api(tags = "挂账功能相关接口")
public class CreditController {

    @Reference(version = "1.0.0", check = false)
    private ICreditService creditService;


    @Reference(version = "1.0.0",check = false)
    private ICreditLogService logService;

    @Reference(version = "1.0.0",check = false)
    private ICreditRepaymentService  repaymentService;

    @PostMapping("/add")
    @ApiOperation("新增挂账")
    public boolean addCredit(@RequestBody CreditVO creditVO) {

        //将vo转为po
        Credit credit = new Credit();
        BeanUtils.copyProperties(creditVO, credit, "users");

        //判断挂账类型是公司还是个人
        if (creditVO.getUsers() != null && !creditVO.getUsers().isEmpty()) {
            //说明里面有数据，挂账类型是公司
            List<CreditCompanyUserVO> users = creditVO.getUsers();
            List<CreditCompanyUser> creditCompanyUsers = new ArrayList<>(users.size());

            for (CreditCompanyUserVO user : users) {
                CreditCompanyUser companyUser = new CreditCompanyUser();
                BeanUtils.copyProperties(user, companyUser);

                creditCompanyUsers.add(companyUser);
            }
            //批量保存
            creditService.add(credit, creditCompanyUsers);

        }

        creditService.add(credit, null);
        return true;
    }

    @ApiOperation("挂账管理列表")
    @GetMapping("/pageList/{page}/{pageSize}")
    public PageVO<CreditVO> pageList(@PathVariable int page,@PathVariable int pageSize,@RequestParam(value = "name",defaultValue = "",required = false) String name){

        IPage<Credit> creditIPage = creditService.queryPage(page, pageSize, name);

        List<CreditVO> voList = Lists.newArrayList();

        try {
            voList = BeanListUtils.copy(creditIPage.getRecords(),CreditVO.class);
        } catch (Exception e) {
            throw new BussinessException("集合转换出错");
        }

        return  new PageVO<CreditVO>(creditIPage,voList);
    }


    @ApiOperation("数据回显")
    @GetMapping("/{id}")
    public CreditVO queryById(@PathVariable String id){

        Credit credit = creditService.queryById(id);

        CreditVO vo = new CreditVO();
        //拷贝属性
        BeanUtils.copyProperties(credit,vo);

        return vo;
    }

    @ApiOperation("修改挂账信息")
    @PutMapping("/update/{id}")
    public boolean updateCredit(@PathVariable String id, @RequestBody CreditVO creditVO){

        //将vo转为po
        Credit credit = creditService.getById(id);

        BeanUtils.copyProperties(creditVO,credit,"users");

        //查询其他属性
        if (creditVO.getUsers() != null && creditVO.getUsers().isEmpty()){

            List<CreditCompanyUser> list = Lists.newArrayList();

            list.forEach(cu -> {
                CreditCompanyUser companyUser = new CreditCompanyUser();
                BeanUtils.copyProperties(cu,companyUser);
                list.add(companyUser);
            });
            return creditService.updateInfo(credit,list);
        }

        return creditService.updateInfo(credit,null);
    }


    @ApiOperation("查询挂账明细")
    @GetMapping("/creditLog/{page}/{pageSize}/{creditId}")
    public PageVO<CreditLogs> pagequery(@PathVariable int page, @PathVariable int pageSize, @PathVariable String creditId){
        return new PageVO<CreditLogs>(logService.queryPage(creditId, page, pageSize));
    }

    @ApiOperation("新增还款记录")
    @PostMapping("/repayment")
    public boolean repayment(@RequestBody CreditRepaymentVO repaymentVO){

        CreditRepayment creditRepayment = new CreditRepayment();

        BeanUtils.copyProperties(repaymentVO,creditRepayment);

        return repaymentService.repayment(creditRepayment);
    }


}
